/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.UI.WebControls;

using DotNetNuke.ModuleToolkit.Common;
using DotNetNuke.ModuleToolkit.Modules.Skinning;

#endregion

namespace DotNetNuke.ModuleToolkit.Modules.Skinning.SkinObjects
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The BaseDataListSkinObject provides a skin object base for lists of items displays a List of Articles
    /// </summary>
    /// <history>
    /// 	[cnurse]	01/06/2007	created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class BaseDataListSkinObject : DataList, ISkinObject
    {

        #region Private Members

        private string _ListSourceID = Null.NullString;

        private SkinnedControl _ParentControl;
        private SkinnablePortalModuleBase _ParentModule;

        private bool _ShowPager = true;

        #endregion

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Associated DataSource
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/13/2007  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected BaseDataSource BaseDataSource
        {
            get
            {
                BaseDataSource dataSource = null;
                if (!String.IsNullOrEmpty(ListSourceID))
                {
                    dataSource = ControlUtils.FindControl(this, ListSourceID) as BaseDataSource;
                }
                return dataSource;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Current Page
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected int CurrentPage
        {
            get
            {
                int _CurrentPage = 1;
                if (BaseDataSource != null)
                    _CurrentPage = BaseDataSource.CurrentPage;
                return _CurrentPage;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets a flag that indicates whether to show the pager
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected bool IsPaged
        {
            get
            {
                bool _IsPaged = Null.NullBoolean;
                if (BaseDataSource != null)
                    _IsPaged = BaseDataSource.IsPaged;
                return _IsPaged;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Page Size
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected int PageSize
        {
            get
            {
                int _PageSize = Int32.MaxValue;
                if (BaseDataSource != null)
                    _PageSize = BaseDataSource.PageSize;
                return _PageSize;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Provides a reference to the module that this object is contained in
        /// </summary>
        /// <remarks>
        /// This property is null if the control is not sited on a SkinnablePortalModuleBase control
        /// </remarks>
        /// <history>
        /// 	[cnurse]	01/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected SkinnablePortalModuleBase ParentModule
        {
            get
            {
                if (_ParentModule == null)
                    _ParentModule = ControlUtils.FindParent<SkinnablePortalModuleBase>(this);
                return _ParentModule;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Querystring parameters to add to the pager
        /// </summary>
        /// <history>
        /// 	[cnurse]	10/30/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected virtual string QueryString
        {
            get
            {
                string queryString = Null.NullString;
                if (!String.IsNullOrEmpty(ParentModule.Scope))
                    queryString = Constants.PARAM_Scope + "=" + ParentModule.Scope;
                if (!String.IsNullOrEmpty(ParentModule.Mode))
                {
                    if (!String.IsNullOrEmpty(queryString))
                        queryString += "&";
                    queryString = Constants.PARAM_Mode + "=" + ParentModule.Mode;
                }

                return queryString;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Total Records
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected int TotalRecords
        {
            get
            {
                int _TotalRecords = 0;
                if (BaseDataSource != null)
                    _TotalRecords = BaseDataSource.TotalRecords;
                return _TotalRecords;
            }
        }

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the ID of the DataSource to use for this skin object
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/13/2007  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(true),
        DefaultValue(true),
        Description("Set the ID of the DataSource to use for this skin object.")]
        public string ListSourceID
        {
            get { return _ListSourceID; }
            set { _ListSourceID = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets whether the Pager is displayed
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/20/2007  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(true),
        DefaultValue(""),
        Description("Set whether to display the Pager control.")]
        public bool ShowPager
        {
            get { return _ShowPager; }
            set { _ShowPager = value; }
        }

        #endregion

        #region Protected Methods

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            DataBind();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// OnPreRender runs just before the Page is Rendered
        /// </summary>
        /// <param name="e">An EventArgs</param>
        /// <history>
        /// 	[cnurse]	01/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Render runs just before the Page is Rendered
        /// </summary>
        /// <param name="writer">The HtmlTextWriter used to render the control</param>
        /// <history>
        /// 	[cnurse]	01/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            if (IsPaged && ShowPager && TotalRecords > PageSize)
            {
                //Render the Pager Control
                PagingControl pager = new PagingControl();
                pager.TotalRecords = TotalRecords;
                pager.PageSize = PageSize;
                pager.CurrentPage = CurrentPage;
                pager.TabID = ParentModule.TabId;

                if (!String.IsNullOrEmpty(QueryString))
                    pager.QuerystringParams = QueryString;

                pager.RenderControl(writer);
            }
        }

        #endregion

        #region ISkinObject Implementation

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Parent SkinnedControl for this SkinObject
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public SkinnedControl ParentControl
        {
            get
            {
                if (_ParentControl == null)
                    _ParentControl = ControlUtils.FindParent<SkinnedControl>(this);
                return _ParentControl;
            }
        }

        #endregion

    }
}
